package cn.yj.netty.service;

import cn.yj.netty.message.eventshander.AbstractMessageHandler;
import cn.yj.netty.support.test.MessageProtocol;
import cn.yj.netty.common.CommandCode;
import cn.yj.netty.common.Const;
import cn.yj.netty.common.MessageType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 业务具体处理：心跳包处理
 *
 * @author 永健
 * @since 2022-02-16 11:34
 */
@Component
public class HeartBeatMessageHandler extends AbstractMessageHandler<MessageProtocol> {
    private static final Logger log = LoggerFactory.getLogger(HeartBeatMessageHandler.class);

    @Override
    public boolean support(MessageProtocol msg) {
        return super.support(CommandCode.HEART_BEAT, msg);
    }

    @Override
    protected void doHandler(MessageProtocol msg) {
        log.info("HeartBeatMessageHandler 接收到消息:心跳包：timestamp={},messageType={},command={},host={}",
                msg.getHeader().getTimestamp(), msg.getHeader().getMessageType(), msg.getHeader().getCommandCode(),
                msg.getHeader().getHost());
        msg.setBody(Const.EMPTY);
        if (MessageType.HEART_BEAT_PING.getCode().equals(msg.getHeader().getMessageType())) {
            msg.getHeader().setMessageType(MessageType.HEART_BEAT_PONG.getCode());
            super.response(msg);
        }
    }
}
